package itbour.onetouchshow.utils.aliyun;

import android.graphics.Bitmap;

import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Random;

import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.utils.DateUtils;
import itbour.onetouchshow.utils.FileUtils;
import itbour.onetouchshow.utils.L_;

/**
 * Created by onetouch on 2017/12/2.
 */

public class AliRequestUtils {
    private static final AliRequestUtils ourInstance = new AliRequestUtils();
    public static String line = "_";
    public static String u = "U";
    public static String li = "/";
    private String name;

    private AliRequestUtils() {
    }

    public static AliRequestUtils getInstance() {
        return ourInstance;
    }

    private static InputStream Bitmap2IS(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        InputStream sbs = new ByteArrayInputStream(baos.toByteArray());
        return sbs;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public PutObjectRequest putObjectRequest(String path) {
        PutObjectRequest put = new PutObjectRequest(AppConst.OSSBUCKETNAME, name, path);
        // 异步上传时可以设置进度回调
        put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
            @Override
            public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
                L_.e("PutObject", "currentSize: " + currentSize + " totalSize: " + totalSize);

            }

        });
        return put;
    }

    /**
     * 把batmap 转file
     *
     * @param bitmap
     */
    public File saveBitmapFile(Bitmap bitmap, String fileName) {
        String newVideoPath = FileUtils.getDownloadDir() + DateUtils.getYMD();
        File file = new File(newVideoPath + fileName);//将要保存图片的路径
        if (file.exists()) {
            file.delete();
        }
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 50, bos);
            bos.flush();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    public String createFileName(String type, String userId) {
        String fileName = type + li + u + userId + li + time() + line + mathRandom();
        return fileName;
    }


    /**
     * 获取系统时间
     *
     * @return
     */
    private String time() {
        long currentTimeMillis = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/HH/mm/ss/SSS");
        String format = sdf.format(currentTimeMillis);
        return format;
    }

    private String mathRandom() {
        Random random = new Random();
        String[] lenth = new String[]{
                "o", "p", "a", "H", "J", "K", "s", "d", "f",
                "g", "h", "j", "k", "l", "z",
                "0", "1", "1", "C", "V", "B",
                "9", "q", "w", "e", "r", "t",
                "y", "u", "i", "x", "c", "v",
                "b", "n", "m", "Q", "W", "E",
                "R", "T", "Y", "U", "I", "O",
                "P", "A", "S", "6", "7", "8",
                "D", "F", "G", "3", "4", "5",
                "L", "Z", "X", "N", "M"};
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < 20; i++) {
            int s = random.nextInt(lenth.length);
            stringBuffer.append(lenth[s]);
        }

        return stringBuffer.toString();
    }


}
